#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>

int main()
{
  pid_t id = fork();
  if(id<0)
  {
    perror("fork fail");
    return 1;
  }
  else if(id==0)
  {
    int cnt = 5;
    while(cnt--)
    {
      printf("i am child pid:%d,ppid:%d,cnt:%d\n",getpid(),getppid(),cnt);
      sleep(1);
    }
      exit(1);
  }
   else
    {
      int cnt = 5;
      while(cnt--)
      {
        printf("i am parent pid:%d,ppid:%d,cnt%d\n",getpid(),getppid(),cnt);
        sleep(1);
      }
      int status = 0;
      while(1)   //轮询
      { 
      pid_t ret = waitpid(id,&status,WNOHANG);    //非阻塞
      if(ret>0)
      {
        if(WIFEXITED(status))          
        {
           printf("进程正常跑完，退出码是：%d\n",WEXITSTATUS(status));
        }
        else 
        {
          printf("进程异常\n"); 
        }
        break;
      }
      else if(ret<0)
      {
        printf("wait fail\n");
        break;
      }
      else 
      {
        printf("你好了没？子进程还没有退出，我在等待\n");
        sleep(1);
      }
      sleep(3);
     }
    }
  return 0;
}

/*
int main()
{
  pid_t id = fork();
  if(id<0)
  {
    perror("fork fail");
    return 1;
  }
  else if(id==0)
  {
    int cnt = 3;
    while(cnt--)
    {
      printf("i am child pid:%d,ppid:%d cnt:%d\n",getpid(),getppid(),cnt);
      sleep(1);
    }
    exit(1);
  }
  else
  {
    int cnt = 5;
    while(cnt--)
    {
      printf("i am parent pid:%d,ppid:%d cnt:%d\n",getpid(),getppid(),cnt);
      sleep(1);
    }
    int status = 0;
    pid_t ret = waitpid(-1,&status,0);
    if(ret==id)
    {
      printf("wait success ret：%d status:%d\n",ret,status);
     //终止信号：7F:0111 1111   退出状态：右移8位，按位与0xFF：1111 1111
     // printf("wait success ret:%d exit sig:%d exit code:%d\n",ret,status&0x7F,(status >> 8) & 0xFF);
      if(WIFEXITED(status))
      {
        printf("进程正常跑完，退出码是：%d\n",WEXITSTATUS(status));
      }
      else 
      {
        printf("进程异常\n")；
      }
      else 
      {
        printf("wait fail\n");
      }
    }
    sleep(1);
  }
  return 0;
}
*/



/*
#define N 5
void run()
{
  int cnt = 5;
  while(cnt)
  {
    printf("i am Child pid:%d,ppid:%d\n",getpid(),getppid());
    sleep(1);
    cnt--;
  }
}
int main()
{
  for(int i=0;i<N;i++)
  {
    pid_t id = fork();
    {
      if(id==0)
      {
        run();
        exit(0);
      }
      //父进程
      printf("create child process:%d  success\n",id);
    }
    sleep(5);
  }
  //等待
  for(int i=0;i<N;i++)
  {
    pid_t id = wait(NULL);
    if(id>0)
    {
      printf("wait %d success\n",id);
    }
  sleep(5);
  }
  return 0;
}
*/

/*
void show()
{
  printf("hello world ");
  printf("hello world ");
  fflush(stdout);
  _exit(11);
  printf("hello linux ");
  printf("hello linux ");
  printf("hello linux ");
}
int main()
{
  show();
  printf("hello ");
  return 3;
}
*/

/*
int main()
{
  pid_t id = fork();
  if(id<0)
  {
    perror("fork fail");
    return 1;
  }
  else if(id==0)
  {
   int cnt = 3;
    while(cnt)
    {
      printf("i am child pid:%d ppid:%d cnt:%d\n",getpid(),getppid(),cnt);
      sleep(1);
      cnt--;
    }
  exit(1);      //只执行子进程的代码
  }
  else  
  {
    int cnt = 5;
    while(cnt)
    { 
       printf("i am parent pid:%d ppid:%d cnt:%d\n",getpid(),getppid(),cnt);
       cnt--;
       sleep(1);
    }

    //等待子进程的回收
    pid_t ret = wait(NULL);
    if(ret==id)
    {
      printf("wait success,ret:%d,\n",ret);
    }

    //回收后在等待5秒
    sleep(5);
  }
  return 0;
}
*/
